package com.baidu.ueditor;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baidu.ueditor.define.ActionMap;

/**
 * 配置管理器
 * 
 * @author hancong03@baidu.com
 *
 */
public final class ConfigManager {

	private final String rootPath;
	public final String contextPath;
	private JSONObject jsonConfig = null;
	// 涂鸦上传filename定义
	private final static String SCRAWL_FILE_NAME = "scrawl";
	// 远程图片抓取filename定义
	private final static String REMOTE_FILE_NAME = "remote";

	/*
	 * 通过一个给定的路径构建一个配置管理器， 该管理器要求地址路径所在目录下必须存在config.properties文件
	 */
	private ConfigManager(String rootPath, String contextPath, String uri) throws FileNotFoundException, IOException {
		rootPath = rootPath.replace("\\", "/");
		this.rootPath = rootPath;
		this.contextPath = contextPath;
		this.jsonConfig = UeditorConfig.JSONCONFIG ;

	}

	/**
	 * 配置管理器构造工厂
	 * 
	 * @param rootPath
	 * @param contextPath
	 *            服务器所在项目路径
	 * @param uri
	 *            当前访问的uri
	 * @return 配置管理器实例或者null
	 */
	public static ConfigManager getInstance(String rootPath, String contextPath, String uri) {

		try {
			return new ConfigManager(rootPath, contextPath, uri);
		} catch (Exception e) {
			return null;
		}

	}

	// 验证配置文件加载是否正确
	public boolean valid() {
		return this.jsonConfig != null;
	}

	public JSONObject getAllConfig() {

		return this.jsonConfig;

	}

	public Map<String, Object> getConfig(int type) {

		Map<String, Object> conf = new HashMap<String, Object>();
		String savePath = null;

		try {
			switch (type) {

			case ActionMap.UPLOAD_FILE:
				conf.put("isBase64", "false");
				conf.put("maxSize", this.jsonConfig.getLong("fileMaxSize"));
				conf.put("allowFiles", this.getArray("fileAllowFiles"));
				conf.put("fieldName", this.jsonConfig.getString("fileFieldName"));
				conf.put("uploadFilePath", this.jsonConfig.getString("uploadFilePath")) ;
				conf.put("uploadFileUrl", this.jsonConfig.getString("uploadFileUrl")) ;
				
				savePath = this.jsonConfig.getString("filePathFormat");
				break;

			case ActionMap.UPLOAD_IMAGE:
				conf.put("isBase64", "false");
				conf.put("maxSize", this.jsonConfig.getLong("imageMaxSize"));
				conf.put("allowFiles", this.getArray("imageAllowFiles"));
				conf.put("fieldName", this.jsonConfig.getString("imageFieldName"));
				conf.put("uploadFilePath", this.jsonConfig.getString("uploadFilePath")) ;
				conf.put("uploadFileUrl", this.jsonConfig.getString("uploadFileUrl")) ;
				savePath = this.jsonConfig.getString("imagePathFormat");
				break;

			case ActionMap.UPLOAD_VIDEO:
				conf.put("maxSize", this.jsonConfig.getLong("videoMaxSize"));
				conf.put("allowFiles", this.getArray("videoAllowFiles"));
				conf.put("fieldName", this.jsonConfig.getString("videoFieldName"));
				conf.put("uploadFilePath", this.jsonConfig.getString("uploadFilePath")) ;
				conf.put("uploadFileUrl", this.jsonConfig.getString("uploadFileUrl")) ;
				savePath = this.jsonConfig.getString("videoPathFormat");
				break;

			case ActionMap.UPLOAD_SCRAWL:
				conf.put("filename", ConfigManager.SCRAWL_FILE_NAME);
				conf.put("maxSize", this.jsonConfig.getLong("scrawlMaxSize"));
				conf.put("fieldName", this.jsonConfig.getString("scrawlFieldName"));
				conf.put("isBase64", "true");
				conf.put("uploadFilePath", this.jsonConfig.getString("uploadFilePath")) ;
				conf.put("uploadFileUrl", this.jsonConfig.getString("uploadFileUrl")) ;
				savePath = this.jsonConfig.getString("scrawlPathFormat");
				break;

			case ActionMap.CATCH_IMAGE:
				conf.put("filename", ConfigManager.REMOTE_FILE_NAME);
				conf.put("filter", this.getArray("catcherLocalDomain"));
				conf.put("maxSize", this.jsonConfig.getLong("catcherMaxSize"));
				conf.put("allowFiles", this.getArray("catcherAllowFiles"));
				conf.put("fieldName", this.jsonConfig.getString("catcherFieldName") + "[]");
				conf.put("uploadFilePath", this.jsonConfig.getString("uploadFilePath")) ;
				conf.put("uploadFileUrl", this.jsonConfig.getString("uploadFileUrl")) ;
				savePath = this.jsonConfig.getString("catcherPathFormat");
				break;

			case ActionMap.LIST_IMAGE:
				conf.put("allowFiles", this.getArray("imageManagerAllowFiles"));
				conf.put("dir", this.jsonConfig.getString("imageManagerListPath"));
				conf.put("count", this.jsonConfig.getInteger("imageManagerListSize"));
				conf.put("uploadFilePath", this.jsonConfig.getString("uploadFilePath")) ;
				conf.put("uploadFileUrl", this.jsonConfig.getString("uploadFileUrl")) ;
				break;

			case ActionMap.LIST_FILE:
				conf.put("allowFiles", this.getArray("fileManagerAllowFiles"));
				conf.put("dir", this.jsonConfig.getString("fileManagerListPath"));
				conf.put("count", this.jsonConfig.getInteger("fileManagerListSize"));
				conf.put("uploadFilePath", this.jsonConfig.getString("uploadFilePath")) ;
				conf.put("uploadFileUrl", this.jsonConfig.getString("uploadFileUrl")) ;
				break;

			}
		} catch (Exception e) {

		}

		conf.put("savePath", savePath);
		conf.put("rootPath", this.rootPath);

		return conf;

	}


	private String[] getArray(String key) {
		String[] result = new String[]{} ;
		try {
			JSONArray jsonArray = this.jsonConfig.getJSONArray(key);
			result = new String[jsonArray.size()];

			for (int i = 0, len = jsonArray.size(); i < len; i++) {
				result[i] = jsonArray.getString(i);
			}
		} catch (Exception e) {
			
		}

		return result;

	}

}
